package com.example.draw;

import com.example.struct.YibihuaGraph;
import com.example.view.DisplayJPanel;

import java.awt.*;
/**
 * @author:我叫程晓非
 * @desc:Grid绘图类
 * @date:2021-11-07
 * @version:1.0
 */
public class Grid {
    int level;
    int x;
    int y;
    int height;
    int width;
    int row;
    int col;
    Color color;
    public static final int MAX_WIDTH = 500;
    public static final int MAX_HEIGHT = 500;
    public static final int MIN_WIDTH = 50;
    public static final int MIN_HEIGHT = 50;

    public Grid(int level, int x, int y, int height, int width, Color color) {
        this.level = level;
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.color = color;
        switch (level){
            case YibihuaGraph.LEVEL_EASY:
                this.row = 2;
                this.col = 2;

                break;
            case YibihuaGraph.LEVEL_NORMAL:
                this.row = 3;
                this.col = 3;
                break;
            case YibihuaGraph.LEVEL_HARD:
                this.row = 4;
                this.col = 4;
                break;
            default:
                this.row = 5;
                this.col = 5;
                break;
        }
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
        switch (level){
            case YibihuaGraph.LEVEL_EASY:
                this.row = 2;
                this.col = 2;

                break;
            case YibihuaGraph.LEVEL_NORMAL:
                this.row = 3;
                this.col = 3;
                break;
            case YibihuaGraph.LEVEL_HARD:
                this.row = 4;
                this.col = 4;
                break;
            default:
                this.row = 5;
                this.col = 5;
                break;
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public void setX(int x,int imgX,int imgWidth) {
        if(x >= imgX && x <= imgX+imgWidth - this.width){
            this.x = x;
        }
    }

    public void setY(int y,int imgY,int imgHeight) {
        if(y >= imgY && y <= imgY+imgHeight -this.height){
            this.y = y;
        }
    }

    public void setHeight(int height) {
        if(height >= MIN_HEIGHT && height<=MAX_HEIGHT){
            this.height = height;
        }
    }

    public void setWidth(int width) {
        if(width >= MIN_WIDTH && width<=MAX_WIDTH){
            this.width = width;
        }
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void draw(Graphics g){
        g.setColor(color);

        int h = height/row;
        int w = width/col;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                g.drawRect(x+i*w,y+j*h,w,h);
            }
        }
    }
    //检查中间是否有点，中间有点的就需要两次测量确保结果准确
    public boolean checkExistCenterPoint(Point start,Point end){

        //同x
        if(start.x == end.x){
            if(Math.abs(start.y - end.y) >= 2){
                return true;
            }
        }
        //同y
        if(start.y == end.y){
            if(Math.abs(start.x - end.x) >= 2){
                return true;
            }
        }
        //同斜线
        if(start.x != end.x && Math.abs((start.y - end.y)/(start.x- end.x))== 1){
            if(Math.abs(start.y - end.y)!=1){
                return true;
            }
        }
        return false;

    }
    public Point getMobilePosition(int index,int imgX,int imgY,float scale){
        Point result = new Point();
        int h = height/row;
        int w = width/col;
        int rowGridX = x + (index%(row+1))*w - imgX;
        int colGridY = y + (index/(col+1))*h - imgY;
        result.x = (int)(scale*rowGridX);
        result.y = (int)(scale*colGridY);
        return result;
    }


}
